#ifndef _EXT_BLOCK_LOG_H_
#define _EXT_BLOCK_LOG_H_

#include <stdint.h>
#include "rtctime.h"
#include "log_buff_manage.h"

#ifdef __cplusplus
extern "C"
{
#endif

#define FLASH_BLOCK_ERR_NONE         0
#define FLASH_BLOCK_ERR_PAGE_SIZE    1  
#define FLASH_BLOCK_ERR_BLOCK_SIZE   2  
#define FLASH_BLOCK_ERR_BLOCK_NUMS   3
#define FLASH_BLOCK_ERR_SIZE         4  
#define FLASH_BLOCK_ERR_PAG_PER_BLK  5  
#define FLASH_BLOCK_ERR_LOG_SIZE     6  

typedef struct
{
	int32_t page;
	int16_t site_offset;
	int16_t addr_offset;
} block_log_site_type;

typedef struct{
    uint32_t block_next;			
    uint32_t block_start;			
	uint32_t page_next;
    uint16_t Err;					
    uint8_t Checked;				
	uint8_t full;					
} block_log_info_type;

typedef struct {
	uint32_t block_base;			
    uint16_t block_amount;			
    uint16_t page_per_block;		
    uint16_t page_size;				
	uint16_t log_size;				
    block_log_info_type *Info;		
    void (*Erase)(uint32_t block_addr);		
    void (*Program)(uint32_t block_addr);	
    void (*Read)(uint32_t page, uint16_t offset, uint16_t Len, uint8_t *Data);	
} block_log_cfg_type;

uint16_t block_log_check(const block_log_cfg_type *Cfg);
uint16_t block_log_save(const block_log_cfg_type *Cfg);

uint16_t block_log_head_search(const block_log_cfg_type *Cfg, uint32_t num, uint32_t *page_des);

int32_t block_log_time_search(const block_log_cfg_type * Cfg, sig_time_type time, int8_t mode);

typedef  struct 
{
	const block_log_cfg_type *blk;
	const log_buff_cfg_type *buf;
} block_log_mix_type;

int32_t block_log_mix_time_search(const block_log_mix_type *cfg, sig_time_type time, int8_t mode);

int32_t block_log_asite_to_lsite(const block_log_cfg_type *cfg, block_log_site_type site );
block_log_site_type block_log_lsite_to_asite(const block_log_cfg_type *cfg, int32_t site );
int32_t block_log_site_amount(const block_log_cfg_type *cfg,  int32_t site_start, int32_t site_end);

int8_t block_log_mix_site_check(const block_log_mix_type *cfg,  int32_t site );
int32_t block_log_mix_site_inc(const block_log_mix_type *cfg,  int32_t site, int32_t inc);
int32_t block_log_mix_read(const block_log_mix_type *cfg,  int32_t site, int16_t len_max, void *buff);

#ifdef __cplusplus
}
#endif

#endif
